package list.p2_carddemo;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardDemo {
    public static final String[] suits = {"♠", "♥", "♦", "♣"};

    public List<Card> buyCard() {
        List<Card> cards = new ArrayList<>();
        for (int i = 1; i <= 13; i++) {
            for (String suit : suits) {
                Card card = new Card(suit, i);
                cards.add(card);
            }
        }
        return cards;
    }

    public void shuffle(List<Card> cards) {
        Random r = new Random();

        for (int i = cards.size() - 1; i >= 0; i--) {
            int index = r.nextInt(52);
            swap(cards, i, index);
        }
    }

    private void swap(List<Card> cards, int i, int j) {
        Card temp = cards.get(i);
        cards.set(i, cards.get(j));
        cards.set(j, temp);
    }

    public List<List<Card>> play(List<Card> cards) {
        List<Card> p1 = new ArrayList<>();
        List<Card> p2 = new ArrayList<>();
        List<Card> p3 = new ArrayList<>();

        for (int i = 0; i < 52; i++) {
            if (i % 3 == 0) {
                p1.add(cards.get(i));
            } else if (i % 3 == 1) {
                p2.add(cards.get(i));
            } else {
                p3.add(cards.get(i));
            }
        }

        return List.of(p1, p2, p3);
    }
}
